package com.winbaoxian.testng.core.data;

import com.winbaoxian.testng.enums.DataPreparationType;
import com.winbaoxian.testng.model.core.DataPreparationConfigDTO;
import com.winbaoxian.testng.model.core.TestCasesContext;
import com.winbaoxian.testng.model.core.log.TestReportDataTestCaseDTO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author dongxuanliang252
 * @date 2019-03-06 14:14
 */
@Component
public class DataPreparationExecutor {

    @Resource
    private TextDataPreparation textDataPreparation;
    @Resource
    private CsvDataPreparation csvDataPreparation;
    @Resource
    private DbDataPreparation dbDataPreparation;

    public List<Map<String, Object>> getPreparationData(TestCasesContext context, DataPreparationConfigDTO config) {
        DataPreparationType type = config.getType();
        TestReportDataTestCaseDTO reportDataContext = context.getReportDataContext();
        try {
            if (DataPreparationType.text.equals(type)) {
                return textDataPreparation.getPreparationData(config, context);
            } else if (DataPreparationType.csv.equals(type)) {
                return csvDataPreparation.getPreparationData(config, context);
            } else if (DataPreparationType.resource.equals(type)) {
                return dbDataPreparation.getPreparationData(config, context);
            }
        } catch (Exception e) {
            reportDataContext.logException(e);
            throw e;
        }
        return null;
    }
}
